using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using ViewOnCore.Device;
using ViewOnCore.HostContract.Tasks;
using ViewOnCore.Helper.Security;
using ViewOnCore.Helper.WPF;
using ViewOnCore.Visual;

#region Copyright (c) 2006, 2007, 2008 ViewOn Media Services. All rights reserved.

/*
 Copyright (c) 2006, 2007, 2008 ViewOn Media Services. All rights reserved.
 This work is copyrighted in its entirety. Permission to use "ViewOn.tv"
 material may be granted only after we receive a written request from you,
 and you receive written permission from us.

 While ViewOn Media Services provides the information herein to anyone,
 we retain copyright on all code, text, graphic images, and other content.
 This means that you may not distribute the code, the text or graphics to others without the
 express written permission of ViewOn Media Services; "mirror" this information on your server
 without our permission; or modify or reuse the code, text or graphics on our servers.
 You may print copies of the information for your own personal use; and, you may reference
 the Service from your own documents. Any commercial use of this material is prohibited without
 the written permission of ViewOn Media Services. In all copies of this information, you must
 retain this notice and any other copyright notices originally included with such information.

 This material may be protected by U.S. and International Copyright laws, and you are legally
 bound by the copyright notices on those systems.

 "ViewOn" logos and other official symbols or graphics may not be used in connection with any
 product or service that has not been expressly authorized in advance in writing or in any manner
 that is likely to cause confusion among the users of this or other Web sites or among the public.
 All other seals, graphics or trademarks of organizations not affiliated with ViewOn Media Services
 that appear on this site are the property of their respective owners.

 ViewOn Media Services reserve all other rights. 
*/

#endregion

#region ViewOn.tv Source Code Licence

/*
This license governs use of the ViewOn.tv software. If you use the software, you accept this license. If you do not accept the license, do not use the software.

1. Summary

The source code is made available to view for reference purposes or to contribute to the project only. Developers may not distribute or modify the code for commercial or non-commercial purposes without express Agreement of View On Media Services.

2. Definitions

The terms "reproduce," "reproduction," and "distribution" have the same meaning here as under U.S. copyright law.
"Licensor" means the company "ViewOn Media Services".
"You" means the licensee of the software, who is not engaged in designing, developing, or testing other software, that has the same or substantially the same features or functionality as the software.
"Your company" means the company you worked for when you downloaded the software.
"Reference use" means use of the software within your company as a reference, in read only form, for the sole purposes of debugging and maintaining your products. For clarity, "reference use" does NOT include (a) the right to use the software for purposes of designing, developing, or testing other software, that has the same or substantially the same features or functionality as the software, and (b) the right to distribute the software outside of your company.
"Licensed patents" means any Licensor patent claims which read directly on the software as distributed by the Licensor under this license.

3. Grant of Rights

(A) Copyright Grant- Subject to the terms of this license, the Licensor grants you a non-transferable, non-exclusive, worldwide, royalty-free copyright license to reproduce the software for reference use.
(B) Patent Grant- Subject to the terms of this license, the Licensor grants you a non-transferable, non-exclusive, worldwide, royalty-free patent license under licensed patents for reference use.
4. Limitations
(A) No Trademark License- This license does not grant you any rights to use the Licensor's name, logo, or trademarks.
(B) If you begin patent litigation against the Licensor over patents that you think may apply to the software (including a cross-claim or counterclaim in a lawsuit), your license to the software ends automatically.
(C) The software is licensed "as-is." You bear the risk of using it. The Licensor gives no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the Licensor excludes the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
 */

#endregion

namespace ViewOnCore.HostContract
{
    public delegate void HostCommandEventHandler(object sender, string commandId, params object[] parameters);

    #region HostCommandIds

    /// <summary>
    /// It is the list of 'Default' commands Ids used in the application.
    /// </summary>
    static public class HostCommandIds
    {
        /// <summary>
        /// Play a media data.
        /// Parameters :
        /// - Media data.
        /// </summary>
        public const string PlayMediaData = "PlayMediaData";
    }

    #endregion

    /// <summary>
    /// The ViewOn.tv Host. It is the class that give access to all the ViewOn.tv
    /// services provided by the application.
    /// 
    /// Everything start here...
    /// </summary>
    static public class Host
    {

        #region Variables

        /// <summary>
        /// The unique instance of the Host's contract
        /// </summary>
        static private IHostContract _instance;

        static private Window _applicationWindow;
        static private Desktop _applicationDesktop;

        //---- Services
        static private IMediaService _mediaService;
        static private IVisualService _visualService;
        static private IUIService _uiService;
        static private IBurningService _burningService;
        static private ISettingsService _settingsService;
        static private IAutoUpdateService _autoUpdateService;

        //---- Tasks management

        /// <summary>
        /// Notify when a task is started
        /// </summary>
        static public event EventHandler TaskStarted;

        /// <summary>
        /// Notify when a task is completed
        /// </summary>
        static public event EventHandler TaskCompleted;

        /// <summary>
        /// The number of tasks currently in the execution queue.
        /// </summary>
        static public int TasksCount;

        //---- Commands management

        /// <summary>
        /// Notify that a component request the execution of a command.
        /// </summary>
        static public event HostCommandEventHandler HostCommand;

        //---- Events
        /// <summary>
        /// This event is raised when the application is completely initialized.
        /// </summary>
        static public event EventHandler ApplicationInitialized;

        #endregion

        #region Properties

        /// <summary>
        /// The unique instance of the host contract.
        /// </summary>
        static public IHostContract Instance
        {
            set
            {
                _instance = value;
            }
            get
            {
                return _instance;
            }
        }

        /// <summary>
        /// The application main window
        /// </summary>
        static public Window ApplicationWindow
        {
            get
            {
                return _applicationWindow;
            }
            set
            {
                _applicationWindow = value;
            }
        }

        /// <summary>
        /// The application main desktop
        /// </summary>
        static public Desktop ApplicationDesktop
        {
            get
            {
                return _applicationDesktop;
            }
            set
            {
                _applicationDesktop = value;
            }
        }

        /// <summary>
        /// This service give access to all the "Media" services.
        /// </summary>
        static public IMediaService MediaService
        {
            get
            {
                return _mediaService;
            }
            set
            {
                _mediaService = value;
            }
        }

        /// <summary>
        /// This service give access to all the "Visual Add Ins" related services.
        /// </summary>
        static public IVisualService VisualService
        {
            get
            {
                return _visualService;
            }
            set
            {
                _visualService = value;
            }
        }

        /// <summary>
        /// This service give access to all the "User interfaces" related services.
        /// </summary>
        static public IUIService UIService
        {
            get
            {
                return _uiService;
            }
            set
            {
                _uiService = value;
            }
        }

        /// <summary>
        /// This service allow to burn on CD and DVDs.
        /// </summary>
        static public IBurningService BurningService
        {
            get
            {
                return _burningService;
            }
            set
            {
                _burningService = value;
            }
        }

        /// <summary>
        /// Define the way to save/retreive the application parameters.
        /// </summary>
        static public ISettingsService SettingsService
        {
            get
            {
                return _settingsService;
            }
            set
            {
                _settingsService = value;
            }
        }

        /// <summary>
        /// This service manage the "auto-update" features
        /// </summary>
        static public IAutoUpdateService AutoUpdateService
        {
            get
            {
                return _autoUpdateService;
            }
            set
            {
                _autoUpdateService = value;
            }
        }

        #endregion

        #region EnqueueTask

        /// <summary>
        /// Request to execute a task.
        /// </summary>
        /// <param name="task">The task to execute</param>
        static public void EnqueueTask(Task task)
        {
            _instance.EnqueueTask(task);
        }

        /// <summary>
        /// Request to execute a task, but it will also cancel
        /// any other task of the same kind that is in the tasks queue.
        /// 
        /// It is used to avoid several kind of operations of the same kinds,
        /// by example to avoid playing several song together.
        /// </summary>
        /// <param name="task">The task to execute</param>
        static public void EnqueueUniqueTask(Task task)
        {
            _instance.EnqueueUniqueTask(task);
        }

        #endregion

        #region CreateEncrypter

        /// <summary>
        /// Create an encrypter for XML documents.
        /// </summary>
        /// <returns></returns>
        static public XmlEncrypter CreateEncrypter()
        {
            return _instance.CreateEncrypter();
        }

        #endregion

        #region InsureSerialRegistration

        /// <summary>
        /// This method insure that all the external component natively
        /// used by ViewOn.tv are registered correctly for the current AddIn.
        /// </summary>
        /// <param name="bassNetType">The type of the class "Un4seen.Bass.BassNet" used for Bass.NET registration.</param>
        /// <param name="sharePodLibType">The type of the class "SharePodLib.SharePodLib" used for SharePodLib registration.</param>
        static public void InsureSerialRegistration(Type bassNetType, Type sharePodLibType)
        {
            _instance.InsureSerialRegistration(bassNetType, sharePodLibType);
        }

        #endregion

        #region OnTaskCompleted

        /// <summary>
        /// Called when a task is complete.
        /// </summary>
        /// <param name="task">The completed task</param>
        static public void OnTaskCompleted(Task task)
        {
            _instance.OnTaskCompleted(task);
        }

        #endregion

        #region NotifyTaskStarted

        /// <summary>
        /// Used by concrete class to notify that a task has started.
        /// </summary>
        /// <param name="task">The task</param>
        static public void NotifyTaskStarted(Task task)
        {
            if (TaskStarted != null)
                TaskStarted(task, null);
        }

        #endregion

        #region NotifyTaskCompleted

        /// <summary>
        /// Used by concrete class to notify that a task is complete.
        /// </summary>
        /// <param name="task">The task</param>
        static public void NotifyTaskCompleted(Task task)
        {
            if (TaskCompleted != null)
                TaskCompleted(task, null);
        }

        #endregion

        #region SendCommand

        /// <summary>
        /// Send a generic command to the host. Any component of the application can
        /// handle the related event and process it.
        /// </summary>
        /// <param name="sender">The component that request the command</param>
        /// <param name="commandId">The unique identifier of the command</param>
        /// <param name="parameters">A list of parameters</param>
        static public void SendCommand(object sender, string commandId, params object[] parameters)
        {
            if (HostCommand != null)
                HostCommand(sender, commandId, parameters);
        }

        #endregion

        #region OnApplicationInitialized

        /// <summary>
        /// This method is called once the application is initialized.
        /// </summary>
        static public void OnApplicationInitialized()
        {
            if (ApplicationInitialized != null)
                ApplicationInitialized(null, null);
        }

        #endregion

    }
}
